import React, { useEffect, useRef, useState, useMemo, useContext } from 'react';
import { SchemaManage } from '@/components/SchemaComponent';
import { connect } from 'umi';
import { AssetacquisitionManageAPI } from '../common/type';
import { getTableColumns } from '../mconfig';
import { PlusOutlined, UploadOutlined } from '@ant-design/icons';
import { MaterialManageServices } from '../services';
import { history } from 'umi';
import { fieldFlowExport, getKeyRouter, getTenantID, getUserDepName, getUserName } from '@/utils';
import { ConractControllerContext } from '../common';

const AssetAcquisitionManageView: React.FC<AssetacquisitionManageAPI.IMaterialManageView> = ({ curRes }) => {
  const schemaExpertsManageRef: any = useRef();

  /**管理页services*/
  const { data, loading, searchList, pagination, reloadRequest, manageTableParams } = MaterialManageServices(curRes);

  /**
   * 获取上下文
   */
  const {
    // 表格数据及参数
    rightTableProps: { dataSource },
  } = useContext(ConractControllerContext);

  /**管理页的ref*/
  const MaterialManageViewRef: any = useRef();

  /**新增弹窗的显示*/
  const [isShowAddModal, setIsShowAddModal] = useState({
    isShow: false,
    recordObj: {},
    clickType: '',
  });

  /**是否显示详情抽屉*/
  const [isShowDetail, setIsShowDetail] = useState<Record<string, any>>({
    isShow: false,
    recordObj: {},
  });

  /**是否显示作废弹窗*/
  const [isShowCancelModal, setIsShowCancelModal] = useState<Record<string, any>>({
    isShow: false,
    recordObj: {},
  });

  /**发布，申请弹窗*/
  const [isShowApplyModal, setIsShowApplyModal] = useState<Record<string, any>>({
    isShow: false,
    recordObj: {},
    type: null,
    select: null,
  });

  /**切换头部方法*/
  const onSwitch = (key: string | undefined | number, searchValues: any, params: any) => {
    reloadRequest({ ...params, ...searchValues });
  };

  /**重置方法*/
  const onReset = (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
    // reloadRequest({ ...reqParams });
  };

  /**搜索方法*/
  const onSearch = (reqParams: AssetacquisitionManageAPI.IAssetTableRequestType) => {
    // reloadRequest({ ...reqParams });
  };

  /**分页方法*/
  const onPageChange = (current: any, pageSize: any, params: any) => {
    // reloadRequest({ pageSize, current, ...params });
  };

  /**Table操作列的事件池*/
  const getTableOperateFuncs: AssetacquisitionManageAPI.IMaterialTableActionType = {
    /**详情按钮*/
    onView(record) {
      history.push({
        pathname: `/assetApply/return/detail/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'view',
          record,
        },
      });
    },
    /**编辑按钮*/
    onEdit(record) {
      history.push({
        pathname: `/assetApply/return/edit/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'edit',
          record,
        },
      });
    },
    /**提交按钮 */
    onSubmit(record) {},
    /**作废按钮 */
    onCancel(record) {},
  };

  /**Table的columns集合*/
  const getTableColumn: Record<string, any>[] = useMemo(() => {
    return getTableColumns(curRes, getTableOperateFuncs);
  }, [curRes]);

  /**新增操作按钮*/
  const actionList: AssetacquisitionManageAPI.IActionList = useMemo(() => {
    // 获取发起人信息
    const record = {
      createrName: getUserName(), // 发起人
      createDepartmentName: getUserDepName(), // 发起人单位
    };
    return [
      {
        title: '添加',
        visible: true,
        type: 'primary',
        icon: <PlusOutlined />,
        onClick: () => {
          history.push({
            pathname: `/assetApply/return/add/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'add',
              record,
            },
          });
        },
      },
    ];
  }, [curRes]);

  return (
    <>
      <SchemaManage
        ref={MaterialManageViewRef}
        searchProps={{
          switchList: [], //头部切换列表
          searchList,
          onSwitch,
          onReset,
          onSearch,
        }}
        actionList={actionList}
        tableProps={{
          columns: getTableColumn,
          dataSource: dataSource, //mockData
          rowKey: 'id',
          loading: loading,
          pagination: pagination as any,
          onPageChange: onPageChange,
        }}
      />
    </>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(AssetAcquisitionManageView);
